//
//  MainTabbarController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/11.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class MainTabbarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBar.tintColor = UIColor.black
        NotificationCenter.default.addObserver(self, selector: #selector(callLogin(not:)), name: NSNotification.Name("needLogin"), object: nil)
        // Do any additional setup after loading the view.
    }

    @objc func callLogin(not: Notification?) -> Void {
        if let number = not!.object as? NSNumber {
            if number.intValue == 1 {
                DispatchQueue.main.async {
                    self.alertSomeMessage(title: YXAlertTitle, content: "请登录!", sureTitle: YXConfirmTitle, cancelTitle: nil, ensureAction: { (alert) in
                        let login = LoginViewController.init()
                        self.present(login, animated: true, completion: nil)
                    }, cancelAction: { (alert) in
                        
                    })
                }
                return
            }else if number.intValue == 2 {
                DispatchQueue.main.async {
                    self.alertSomeMessage(title: YXAlertTitle, content: "账户过期,请重新登录!", sureTitle: YXConfirmTitle, cancelTitle: nil, ensureAction: { (alert) in
                        let login = LoginViewController.init()
                        self.present(login, animated: true, completion: nil)
                    }, cancelAction: { (alert) in
                        
                    })
                }
                return
            }
        } else {
            DispatchQueue.main.async {
                let login = LoginViewController.init()
                self.present(login, animated: true, completion: nil)
            }
            return
        }
    }
    
    internal func alertSomeMessage(title: String?, content: String?, sureTitle: String?, cancelTitle: String?, ensureAction: @escaping (UIAlertAction) -> Void, cancelAction: @escaping (UIAlertAction) -> Void) {
        let alertConCtl = UIAlertController.init(title: title, message: content, preferredStyle: UIAlertControllerStyle.alert)
        let ensureAction = UIAlertAction.init(title: sureTitle, style: UIAlertActionStyle.default, handler: ensureAction)
        if(cancelTitle != nil){
            let cancelAction = UIAlertAction.init(title: cancelTitle, style: UIAlertActionStyle.cancel, handler: cancelAction)
            alertConCtl.addAction(cancelAction)
        }
        alertConCtl.addAction(ensureAction)
        self.present(alertConCtl, animated: true) {
            
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
    deinit{
        NotificationCenter.default.removeObserver(self)
    }
}
